package localhost._8080.metaimage;

import gui.UI;

import java.awt.List;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.xml.namespace.QName;

import metaphotoshop.MetaDrawService;
import metaphotoshop.Metaphotoshop;

public class Client {
	
	public static String id = null;
	public static Metaphotoshop p = null;
	
	public static void main(String[] args) {
		
		MetaDrawService ds = null;
		
		//GUI g = new GUI();
		
		try {
			ds = new MetaDrawService(new URL("http://localhost:8080/metaphotoshop?WSDL"), new QName("http://metaphotoshop/", "MetaDrawService"));
			p = ds.getMetaphotoshopPort();

//			p.logIn("jerome");
//			System.out.println(p.getXML("jerome", "img2"));
//			System.out.println("This is the end, my only friend, the End !");
			
			/*do {
				InputStreamReader isr = new InputStreamReader(System.in); 
				BufferedReader br=new BufferedReader(isr); 
				System.out.println("Welcome to the image managing web service");
				System.out.println("Authors : Jérôme Delamarche & Olivier Le Ster\n");
				System.out.print("Please, enter your ID : ");
				id = br.readLine();
			} while(p.logIn(id) != true);
			System.out.println("--info : ACCESS GRANTED");
			System.out.println("Hi "+id+", welcome back !");*/
		
			//Schedule a job for the event-dispatching thread:
	        //creating and showing this application's GUI.
	        javax.swing.SwingUtilities.invokeLater(new Runnable() {
	            public void run() {
	                UI.createAndShowGUI();
	            }
	        });
	       /* Thread t = new Thread(new SplitPaneDemo2());
	        t.start();*/
	        
	        
	        
	        
	        
	        
	        /*
	        
			while(true) {
				*/
		        /*BufferedReader entree = new BufferedReader(new StringReader(SplitPaneDemo2.label.getText()));
		        String ligne = entree.readLine();
		        SplitPaneDemo2.print(id+"@client:");
		        System.out.println(ligne);*/
		        
			/*	InputStreamReader isr = new InputStreamReader(System.in); 
				BufferedReader br=new BufferedReader(isr); 
				System.out.print(id+"@client:");
				String line = br.readLine();
				
				String[] cmd = line.split("\\s");
				
				if(cmd[0].equalsIgnoreCase("logIn") && cmd.length == 2) {
					p.logIn(cmd[1]);
				}
				
				if(cmd[0].equalsIgnoreCase("logOff") && cmd.length == 1) {
					System.out.println("Bye, "+id+" !");
					System.out.println("--info : Deconnected from server");
					id = null;
					return;
				}
				
				if(cmd[0].equalsIgnoreCase("createImage") && cmd.length == 2) {
					p.createImage(cmd[1], id);
				}
				
				if(cmd[0].equalsIgnoreCase("addArc") && cmd.length == 9) {
					if(p.isGrantedUser(id, "addArc", cmd[1], null)) {
						p.addArc(cmd[1], 
									line, Integer.parseInt(cmd[2]), 
									Integer.parseInt(cmd[3]), 
									Integer.parseInt(cmd[4]), 
									Integer.parseInt(cmd[5]), 
									Integer.parseInt(cmd[6]), 
									Integer.parseInt(cmd[7]), 
									Integer.parseInt(cmd[8]));
					}
					else { System.out.println("--info : You are not allowed to do this !"); }
				}
				
				if(cmd[0].equalsIgnoreCase("addEllipse") && cmd.length == 6) {
					if(p.isGrantedUser(id, "addEllipse", cmd[1], null)) {
						p.addEllipse(cmd[1], 
								line, Integer.parseInt(cmd[2]), 
								Integer.parseInt(cmd[3]), 
								Integer.parseInt(cmd[4]), 
								Integer.parseInt(cmd[5]));
					}
					else { System.out.println("--info : You are not allowed to do this !"); }
				}
				
				if(cmd[0].equalsIgnoreCase("addLine") && cmd.length == 6) {
					if(p.isGrantedUser(id, "addLine", cmd[1], null)) {
						p.addLine(cmd[1], line, Integer.parseInt(cmd[2]), Integer.parseInt(cmd[3]), Integer.parseInt(cmd[4]), Integer.parseInt(cmd[5]));
					}
					else { System.out.println("--info : You are not allowed to do this !"); }
				}
				
				//addPolygon
				//TODO
				
				if(cmd[0].equalsIgnoreCase("addRectangle") && cmd.length == 6) {
					if(p.isGrantedUser(id, "addRectangle", cmd[1], null)) {
						p.addRectangle(cmd[1], 
								line, Integer.parseInt(cmd[2]), 
								Integer.parseInt(cmd[3]), 
								Integer.parseInt(cmd[4]), 
								Integer.parseInt(cmd[5]));
					}
					else { System.out.println("--info : You are not allowed to do this !"); }
				}
				
				if(cmd[0].equalsIgnoreCase("addImage") && cmd.length == 3) {
					if(p.isGrantedUser(id, "addImage", cmd[1], cmd[2])) {
						p.addImage(cmd[1], cmd[2], line);
					}
					else { System.out.println("--info : You are not allowed to do this !"); }
				}
				
				if(cmd[0].equalsIgnoreCase("getXML") && cmd.length == 2) {
					if(p.isGrantedUser(id, "getXML", cmd[1], null)) {
						System.out.println(p.getXML(cmd[1], line)+"\n");
					}
					else { System.out.println("--info : You are not allowed to do this !"); }
				}
				
				if(cmd[0].equalsIgnoreCase("getImagePng") && cmd.length == 2) {
					if(p.isGrantedUser(id, "getImagePng", cmd[1], null)) {
						InputStream in = new ByteArrayInputStream(p.getImagePng(cmd[1], line));
						BufferedImage bImageFromConvert = ImageIO.read(in);
						ImageIO.write(bImageFromConvert, "png", new File(cmd[1]+".png"));
					}
					else { System.out.println("--info : You are not allowed to do this !"); }
				}
				
				if(cmd[0].equalsIgnoreCase("removeItem") && cmd.length == 3) {
					if(p.isGrantedUser(id, "removeItem", cmd[1], null)) {
						p.removeItem(cmd[1], cmd[2], line);
					}
					else { System.out.println("--info : You are not allowed to do this !"); }
				}	
				
				if(cmd[0].equalsIgnoreCase("deleteItem") && cmd.length == 2) {
					if(p.isGrantedUser(id, "deleteItem", cmd[1], null)) {
						p.deleteItem(cmd[1], line);
					}
					else { System.out.println("--info : You are not allowed to do this !"); }
				}
			}*/
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	//Execute la commande passée en paramètre
	public static String execute (String line){		
		String[] cmd = line.split("\\s");
	
		if(cmd[0].equalsIgnoreCase("logIn") && cmd.length == 2) {
			p.logIn(cmd[1]);
		}
		
		/*else if(cmd[0].equalsIgnoreCase("logOff") && cmd.length == 1) {
			System.out.println("Bye, "+id+" !");
			
			System.out.println("--info : Deconnected from server");
			id = null;
			return null;
		}*/
		
		else if(cmd[0].equalsIgnoreCase("createImage") && cmd.length == 2) {
			p.createImage(cmd[1], id);
		}
		
		else if(cmd[0].equalsIgnoreCase("addArc") && cmd.length == 9) {
			if(p.isGrantedUser(id, "addArc", cmd[1], null)) {
				p.addArc(id, 
							cmd[1], Integer.parseInt(cmd[2]), 
							Integer.parseInt(cmd[3]), 
							Integer.parseInt(cmd[4]), 
							Integer.parseInt(cmd[5]), 
							Integer.parseInt(cmd[6]), 
							Integer.parseInt(cmd[7]), 
							Integer.parseInt(cmd[8]));
			}
			else { System.out.println("--info : You are not allowed to do this !");
			return "--info : You are not allowed to do this !";
			}
		}
		
		else if(cmd[0].equalsIgnoreCase("addEllipse") && cmd.length == 6) {
			if(p.isGrantedUser(id, "addEllipse", cmd[1], null)) {
				p.addEllipse(id, 
						cmd[1], Integer.parseInt(cmd[2]), 
						Integer.parseInt(cmd[3]), 
						Integer.parseInt(cmd[4]), 
						Integer.parseInt(cmd[5]));
			}
			else { System.out.println("--info : You are not allowed to do this !");
			return "--info : You are not allowed to do this !";
			}
		}
		
		else if(cmd[0].equalsIgnoreCase("addLine") && cmd.length == 6) {
			if(p.isGrantedUser(id, "addLine", cmd[1], null)) {
				p.addLine(id, cmd[1], Integer.parseInt(cmd[2]), Integer.parseInt(cmd[3]), Integer.parseInt(cmd[4]), Integer.parseInt(cmd[5]));
			}
			else { System.out.println("--info : You are not allowed to do this !");
			return "--info : You are not allowed to do this !";
			}
		}
		
		//addPolygon
		//TODO
		else if (cmd[0].equalsIgnoreCase("addPolygon") && cmd.length >=5){
			if(p.isGrantedUser(id, "addPolygon", cmd[1], null)) {
				
				int polLength =Integer.parseInt( cmd[cmd.length-1]);				
				ArrayList<Integer> x = new ArrayList<Integer>();
				ArrayList<Integer> y = new ArrayList<Integer>();
				
				for(int j=0;j<polLength;j++){
					x.add(j, Integer.parseInt(cmd[2+j]));
					y.add(j,Integer.parseInt(cmd[2+j+polLength]));
					
				}
				
				p.addPolygon(id,cmd[1], x, y, polLength);
				
			}
		}
		
		else if(cmd[0].equalsIgnoreCase("addRectangle") && cmd.length == 6) {
			if(p.isGrantedUser(id, "addRectangle", cmd[1], null)) {
				p.addRectangle(id, cmd[1], Integer.parseInt(cmd[2]), 
						Integer.parseInt(cmd[3]), 
						Integer.parseInt(cmd[4]), 
						Integer.parseInt(cmd[5]));
			}
			else { System.out.println("--info : You are not allowed to do this !");
			return "--info : You are not allowed to do this !";
			}
		}
		
		else if(cmd[0].equalsIgnoreCase("addImage") && cmd.length == 3) {
			if(p.isGrantedUser(id, "addImage", cmd[1], cmd[2])) {
				p.addImage(id,cmd[1], cmd[2]);
			}
			else { System.out.println("--info : You are not allowed to do this !");
			return "--info : You are not allowed to do this !";
			}
		}
		
		else if(cmd[0].equalsIgnoreCase("getXML") && cmd.length == 2) {
			if(p.isGrantedUser(id, "getXML", cmd[1], null)) {
				System.out.println(p.getXML(id, cmd[1])+"\n");
			}
			else { System.out.println("--info : You are not allowed to do this !");
			return "--info : You are not allowed to do this !";
			}
		}
		
		else if(cmd[0].equalsIgnoreCase("getImagePng") && cmd.length == 2) {
			if(p.isGrantedUser(id, "getImagePng", cmd[1], null)) {
				InputStream in = new ByteArrayInputStream(p.getImagePng(id, cmd[1]));
				BufferedImage bImageFromConvert;
				try {
					bImageFromConvert = ImageIO.read(in);				
					ImageIO.write(bImageFromConvert, "png", new File(cmd[1]+".png"));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else { System.out.println("--info : You are not allowed to do this !");
			return "--info : You are not allowed to do this !";
	}
		}
		
		else if(cmd[0].equalsIgnoreCase("removeItem") && cmd.length == 3) {
			if(p.isGrantedUser(id, "removeItem", cmd[1], null)) {
				p.removeItem(id,cmd[1], cmd[2]);
			}
			else { System.out.println("--info : You are not allowed to do this !");
			return "--info : You are not allowed to do this !";
	}
		}	
		
		else if(cmd[0].equalsIgnoreCase("deleteItem") && cmd.length == 2) {
			if(p.isGrantedUser(id, "deleteItem", cmd[1], null)) {
				p.deleteItem(id,cmd[1]);
			}
			else { System.out.println("--info : You are not allowed to do this !");
					return "--info : You are not allowed to do this !";
			}
		}
		else return new String("Error, command not allowed");
		
		return new String("Command succeed");
	}
}

// OLD STUFF


//p.createMetaImage("UneMetaImage", 500, 500);
/*
p.createImage("UneImage");
//p.addLine("UneImage", 0, 0, 500, 500);
p.addRectangle("UneImage", 100, 100, 100, 200);
//p.addArc("UneImage", 200, 100, 200, 100, 20, 160, 0);

p.createImage("UneImage3");

p.createImage("UneImage2");
//p.addLine("UneImage2", 500, 0, 0, 500);
p.addRectangle("UneImage2", 200, 200, 200, 200);
p.addArc("UneImage2", 200, 100, 200, 100, 20, 160, 0);

p.addImage("UneImage", "UneImage2");

//p.addImageInMetaImage("UneMetaImage", "UneImage");

System.out.println(p.getXML("UneImage"));
//p.removeItem("UneMetaImage", "toto");
p.addLine("UneImage2", 500, 0, 0, 500);
//p.removeItem("UneImage2", "toto");
//p.removeItem("UneImage", "UneImage2");
p.addLine("UneImage3", 0, 0, 500, 500);
System.out.println(p.getXML("UneImage"));

//convert byte array back to BufferedImage
InputStream in = new ByteArrayInputStream(p.getImagePng("UneImage"));
BufferedImage bImageFromConvert = ImageIO.read(in);

ImageIO.write(bImageFromConvert, "png", 
         new File("received_image.png")); 
*/